package com.ruanda.spongebobrunner.ui;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.MediaPlayer;
import lombok.Data;

/**
 * 道具实体类
 */
@Data
public class PowerUp {
    public static final int TYPE_LIFE = 0; // 生命道具

    private Rect bounds;
    private int type;
    private Bitmap image;
    private boolean active;
    private float spawnProbability; // 出现概率，用于根据难度调节

    private Paint paint;

    private MediaPlayer sound; // 音效

    public PowerUp(int x, int y, int width, int height, int type, Bitmap image, float spawnProbability,
            MediaPlayer sound) {
        bounds = new Rect(x, y, x + width, y + height);
        this.type = type;
        this.image = image;
        this.spawnProbability = spawnProbability;
        this.active = true;
        this.sound = sound;

        paint = new Paint();
        paint.setAntiAlias(true);
    }

    /**
     * 更新道具状态
     */
    public void update(int scrollSpeed) {
        if (active) {
            // 道具随地形一起滚动
            bounds.offset(-scrollSpeed, 0);
        }
    }

    /**
     * 绘制道具
     */
    public void draw(Canvas canvas) {
        if (active && image != null) {
            canvas.drawBitmap(image, null, bounds, paint);
        }
    }

    /**
     * 检查是否与玩家碰撞
     */
    public boolean checkCollision(Rect playerBounds) {
        return active && Rect.intersects(bounds, playerBounds);
    }

    /**
     * 激活道具效果
     */
    public void activate() {
        active = false; // 道具被收集后失效
    }

    /**
     * 检查道具是否在屏幕外
     */
    public boolean isOffScreen() {
        return bounds.right < 0;
    }

    /**
     * 播放获取道具音效
     */
    public void playSound() {
        if (sound != null) {
            sound.seekTo(0);
            sound.start();
        }
    }

    // Getters and Setters
    public Rect getBounds() {
        return bounds;
    }

    public int getType() {
        return type;
    }

    public boolean isActive() {
        return active;
    }

    public float getSpawnProbability() {
        return spawnProbability;
    }

    public void setSpawnProbability(float spawnProbability) {
        this.spawnProbability = spawnProbability;
    }
}